import { showSection } from './utils.js';

// 加载控制面板数据
export async function loadDashboardData() {
    try {
        // 获取用户总数
        const usersResponse = await fetch('/api/users', {
            headers: {
                'Authorization': `Bearer ${localStorage.getItem('access_token')}`
            }
        });
        if (usersResponse.ok) {
            const usersData = await usersResponse.json();
            document.getElementById('total-users').textContent = usersData.length;
        }

        // 获取活跃模型数
        const modelsResponse = await fetch('/api/get_all_llm_models', {
            headers: {
                'Authorization': `Bearer ${localStorage.getItem('access_token')}`
            }
        });
        if (modelsResponse.ok) {
            const modelsData = await modelsResponse.json();
            const activeModels = modelsData.filter(model => model.is_active);
            document.getElementById('active-models').textContent = activeModels.length;
        }

        // 获取文档数量
        const docsResponse = await fetch('/api/documents', {
            headers: {
                'Authorization': `Bearer ${localStorage.getItem('access_token')}`
            }
        });
        if (docsResponse.ok) {
            const docsData = await docsResponse.json();
            document.getElementById('total-docs').textContent = docsData.length;
        }
    } catch (error) {
        console.error('加载控制面板数据错误:', error);
    }
}

// 初始化控制面板
export function initDashboard() {
    loadDashboardData();
}